package ro.gateway.aida.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Hashtable;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.Organization;
import ro.gateway.aida.obj.db.OrganizationDB;
import ro.gateway.aida.srv.EditorServlet;
import ro.gateway.aida.utils.HttpUtils;



/**
 * @author Mihai Postelnicu
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class ViewOrgServlet extends EditorServlet
{

	  /* (non-Javadoc)
	   * @see ro.gateway.aida.srv.EditorServlet#other_action_performed(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String)
	   */
	  protected void other_action_performed(
			HttpServletRequest request,
			HttpServletResponse response,
			String action )
			throws IOException, ServletException
	  {
			// TODO Auto-generated method stub

	  }

	  protected void start_editing(
			ServletContext application,
			HttpServletRequest request,
			HttpServletResponse response ) throws IOException, ServletException
	  {
			super.start_editing( application, request, response );
			PersistenceToken token = PersistenceManager.
						  tokenLookup( this.getServletContext() );
			Hashtable bean = getBean( request );
			String sid = HttpUtils.getValidTrimedString( request, "id", null );
			if ( sid == null ) return;

			OrganizationDB orgdb = OrganizationDB.getManager(token);
			Organization org = null;
			try
			{
				  org = orgdb.getByID( Long.parseLong( sid ) );
			}
			catch ( SQLException e )
			{
				  e.printStackTrace();
			}
			catch ( NumberFormatException e )
			{
				  e.printStackTrace();
			}

			if ( org == null ) return;
			beanSync( bean, "org", org );
			if (request.getParameter("list")!=null) 
				request.setAttribute("mode","list");
			
	  }

	  /* (non-Javadoc)
	   * @see ro.gateway.aida.srv.EditorServlet#confirm(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	   */
	  protected void confirm(
			HttpServletRequest request,
			HttpServletResponse response )
			throws IOException, ServletException
	  {
			// TODO Auto-generated method stub

	  }

	  /* (non-Javadoc)
	   * @see ro.gateway.aida.srv.EditorServlet#save(javax.servlet.ServletContext, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	   */
	  protected void save(
			ServletContext application,
			HttpServletRequest request,
			HttpServletResponse response )
			throws IOException, ServletException
	  {
			// TODO Auto-generated method stub

	  }

	  /* (non-Javadoc)
	   * @see ro.gateway.aida.srv.EditorServlet#edit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	   */
	  protected void edit(
			HttpServletRequest request,
			HttpServletResponse response )
			throws IOException, ServletException
	  {
			// TODO Auto-generated method stub
	  }

	  /* (non-Javadoc)
	   * @see ro.gateway.aida.srv.EditorServlet#getPNAME_BEANS()
	   */
	  public String getPNAME_BEANS()
	  {
			return PNAME_BEANS;
	  }

	  /* (non-Javadoc)
	   * @see ro.gateway.aida.srv.EditorServlet#getJSP_PAGE()
	   */
	  public String getJSP_PAGE()
	  {
			return JSP_PAGE;
	  }


	  public static final String PNAME_BEANS = "VIEWING_ORGANIZATIONS";
	  public static final String JSP_PAGE = "view.jsp";
}

